package flockingGA;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class Parameters implements Serializable{
	 /**
	 * 
	 */
	private static final long serialVersionUID = -6062534881995446304L;
	
	// in [0,100]
    public final double r_sig;
    public final double r_sep;
    public final double r_col;
    public final double r_col_sep;
    public final double r_lin;
    public final double r_lin_sep;
    // in [0,2]
    public final double w_r1_a; // enemy not visible 
    public final double w_r1_b; // enemy in sight range
    public final double w_r2;
    public final double w_r3;
    public final double w_r4;
    
   /*
    * current values:
    r_col	30.561030343081775	
	r_col_sep	94.17452854219717	
	r_lin	9.495339376699762	
	r_lin_sep	52.19615448459229	
	r_sep	94.24331446925851	
	r_sig	57.79648676462603	
	w_r1_a	1.7443868638495728	
	w_r1_b	1.3435746039998744	
	w_r2	1.710104350335109	
	w_r3	0.0829173527809568	
	w_r4	1.0805294375908865	
    */
    
    public Parameters()
    {
    	// default parameters
    	r_sig = 72;
    	r_sep = 15;
    	r_col = 75;
    	r_col_sep = 38;
    	r_lin = 19;
    	r_lin_sep = 49;
    	
    	w_r1_a = 1.78;
    	w_r1_b = 1.35;
    	w_r2 = 1.85;
    	w_r3 = 1.5;
    	w_r4 = 0.45;
    }

    public Parameters(double[] genoms)
    {
    	// all genoms are between [0,1]
    	
    	// in [0,100]
    	r_sig = genoms[0] * 100;
    	r_sep = genoms[1] * 100;
    	r_col = genoms[2] * 100;
    	r_col_sep = genoms[3] * 100;
    	r_lin = genoms[4] * 100;
    	r_lin_sep = genoms[5] * 100;
    	 // in [0,2]
    	w_r1_a = genoms[6] * 2;
    	w_r1_b = genoms[7] * 2;
    	w_r2 = genoms[8] * 2;
    	w_r3 = genoms[9] * 2;
    	w_r4 = genoms[10] * 2;
    }
    
    public void saveToFile()
    {
    	try (
  		      OutputStream file = new FileOutputStream("GA.obj");
  		      OutputStream buffer = new BufferedOutputStream(file);
  		      ObjectOutput output = new ObjectOutputStream(buffer);
  		    )
  		    {
  		      output.writeObject(this);
  		      System.out.println("GA parameters saved");
  		    }  
  		    catch(IOException ex){
  		    	System.out.println(ex.getMessage());
  		    }
    }
    
    public static Parameters loadFromFile()
    {
    	try(
  		      InputStream file = new FileInputStream("GA.obj");
  		      InputStream buffer = new BufferedInputStream(file);
  		      ObjectInput input = new ObjectInputStream (buffer);
  		    ){
  		      Parameters result = (Parameters)input.readObject();
  		      if ( result != null )
  		      {
  		    	  System.out.println("GA parameters loaded");
  		    	  return result;
  		      }
  		    	  
  		    }
  		    catch(ClassNotFoundException ex){
  		    	System.out.println(ex.getMessage());
  		    }
  		    catch(IOException ex){
  		    	System.out.println(ex.getMessage());
  		    }
  	    	return null;
    }
}
